package com.zhao.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhao.bean.Employee;
import com.zhao.bean.PageParam;
import com.zhao.bean.R;
import com.zhao.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService es;
    //登录
    @PostMapping("/employee/login")
    public R login(@RequestBody Employee employee, HttpSession session){
        Employee login = es.login(employee);
        R r=null;
        if (login!=null){
            r=R.success("登录成功");

            session.setAttribute("employee",login);
        }else {
            r=R.error("用户名或密码错误");
        }
        return r;
    }
    //退出登录
    @PostMapping("/employee/logout")
    public R logout(HttpSession session){
        //销毁session
        try {
            session.invalidate();
            return R.success("退出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("退出失敗");
        }

    }
    //添加员工
    @PostMapping("/employee")
    public R add(@RequestBody Employee employee,HttpSession session){
        //获取存放在作用域中登录中的用户信息
        Employee emp = (Employee) session.getAttribute("employee");
       /* //设置添加人
        employee.setCreateUser(emp.getId());
        //设置修改人
        employee.setUpdateUser(emp.getId());
        //设置修改时间
        employee.setCreateTime(LocalDateTime.now());
        //设置添加时间
        employee.setUpdateTime(LocalDateTime.now());*/
        //设置初始密码为123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        System.out.println(":::::::::::::::::::"+employee);
        int row = es.add(employee);

        System.out.println(":::::::::::::::::::"+row);
        R r=null;
        if (row>0){
          r= R.success("创建成功");
        }else {
            r=R.error("创建失败");
        }
        return r;
    }
    //分页查询
    @GetMapping("/employee/{params}")
    public R find(@PathVariable String params){
        System.out.println("::::::::::::::::::::::::"+params);
        //把json字符串转化成一个JavaBean对象,因为现在传过来的数据无法取值
        PageParam param = JSON.parseObject(params, PageParam.class);
        System.out.println(param);
        IPage<Employee> page = es.find(param);

        return R.success(page);
    }
    //修改状态
    @PutMapping("/employee")
    public R update(@RequestBody Employee employee,HttpSession session){
        //获取存放在作用域中登录中的用户信息
        Employee emp = (Employee) session.getAttribute("employee");
        //设置修改人
      /*  employee.setUpdateUser(emp.getId());*/
        //设置修改时间
       /* employee.setUpdateTime(LocalDateTime.now());*/

        int update = es.update(employee);
        System.out.println("**********************");
        System.out.println(employee);
        System.out.println("**********************");
        R r=null;
        if (update>0){
            r=R.success("修改成功");
        }else{
            r=R.error("修改失败");
        }
        return r;
    }
}
